$( document ).ready(function() {
	$("#schedule").empty();
	$.getJSON("doctor/lookupAppointment.html", function(data) {
		if(!jQuery.isEmptyObject(data.doctorAppointments)){
			$("#schedule").data("scheduleResult", data);
			printScheduleTableBody(data);
		}else{
			$("#schedule").append("<strong>No result.</strong>");
		}
		
	});
});

function printScheduleTableBody(data){
	$("#schedule")
	.append(
			"<table id=\"scheduleList\" class=\"tablecss\">" +
			"<thead>" +
			"<tr>" +
			"<th>Appointment id</th>" +
			"<th>Patient id</th>" +
			"<th>Patient name</th>" +
			"<th>Doctor id</th>" +
			"<th>Doctor name</th>" +
			"<th>Operate staff id</th>" +
			"<th>Operate staff name</th>" +
			"<th>Appointment time</th>" +
			"</tr>" +
			"</thead>" +
			"<tbody id=\"scheduleTableBody\">" +
			"</tbody>" +
			"</table>");
var tr;
$(data).each(
	function(index, value) {
		$(value.doctorAppointments).each(function (i,v){
			tr = $("<tr>");
			tr.append("<td>" + v.appointmentId + "</td>");
			tr.append("<td>" + v.patientId + "</td>");
			tr.append("<td>" + v.patientName + "</td>");
			tr.append("<td>" + v.doctorId + "</td>");
			tr.append("<td>" + v.doctorName + "</td>");
			tr.append("<td>" + v.staffId + "</td>");
			tr.append("<td>" + v.staffName + "</td>");
			tr.append("<td>" + convertDateTime(v.appointmentTime) + "</td>");
			tr.append("</tr>");
			$("#scheduleTableBody").append(tr);
		});
		
	});
$("#scheduleList").tablesorter({
	widgets : [ 'zebra' ]
});
}

function convertDateTime(milliSec){
	var dateTime=new Date(milliSec);
	var format=new String("");
	format=dateTime.getFullYear()+"-";
	if((dateTime.getMonth()+1)<10){
		format=format+"0"+(dateTime.getMonth()+1)+"-";
	}else{
		format=format+(dateTime.getMonth()+1)+"-";
	}
	if(dateTime.getDate()<10){
		format=format+"0"+dateTime.getDate()+" ";
	}else{
		format=format+dateTime.getDate()+" ";
	}
	if(dateTime.getHours()<10){
		format=format+"0"+dateTime.getHours()+":";
	}else{
		format=format+dateTime.getHours()+":";
	}
	if(dateTime.getMinutes()<10){
		format=format+"0"+dateTime.getMinutes();
	}else{
		format=format+dateTime.getMinutes();
	}
	return format;
}